<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Login</title>
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <script src="http://passport.cnblogs.com/scripts/jsencrypt.min.js"></script>
    <script type="text/javascript">
    	
        $(function () {
        	$.get("/rsa/test/public-key").done(function(data){
        		$("#tra").val(data);
        	});
            var encrypt = new JSEncrypt();
            var reg = new RegExp('/\r\n' , "g" )
            $("#btn").click(function () {
            	var pubKey = $("#tra").val()+"";
            	console.info(pubKey)
            	$("#tra").val(pubKey.replace(/(\\r\\n)/g,"\n"))
            	  encrypt.setPublicKey($("#tra").val());
                  var data = encrypt.encrypt($("#password").val());
                  $("#codePassword").val(data)
                //+号的处理：因为数据在网络上传输时，非字母数字字符都将被替换成百分号（%）后跟两位十六进制数，而base64编码在传输到后端的时候，+会变成空格，因此先替换掉。后端再替换回来
                //表单提交才需要？
                //encodeURI(data).replace(/\+/g, '%2B')
                data = {password:data};
                 data = JSON.stringify(data);
                 console.log("公钥:"+$("#tra").val());
                console.log("提交数据："+data)
                $.ajax({
                    url: '/rsa/test/login',
                    data: data,  
                    type: 'post',
                    contentType:"application/json;charset=utf-8",
                    success: function (msg) {
                        alert("服务端返回："+msg);
                    }
                });
            });

        });


    </script>
</head>
<body>
    <div>
       公钥：
        <textarea id="tra" rows="15" cols="65">
        </textarea>
   密码加密后：<textarea id="codePassword" rows="15" cols="65">
        </textarea><br/>
        密码： <input id="password" type='text' />
        <hr/>
        注意+好的处理
         <input type="button" id="btn" value="点我" />
    </div>
</body>
</html>